package handlers;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
//import org.textmining.*;
//import org.textmining.extraction.word.*;
import org.apache.poi.hdf.extractor.WordDocument;
import org.apache.poi.hwpf.*;

@SuppressWarnings("unused")
public class WordDocHandler implements DocumentHandler{

	@SuppressWarnings("deprecation")
	public Document getDocument(InputStream is) throws Exception {
		
		//WordExtractorFactory o = new WordExtractorFactory();
//		String bodyText = "";
//		try {
//				Word97TextExtractor w =  new Word97TextExtractor(is);
//				bodyText = w.getText();
//		}
//		catch (Exception e) {throw new Exception("Cannot extract text from a Word document", e);
//		}
//		if ((bodyText != null) && (bodyText.trim().length() > 0)) {
//			Document doc = new Document();
//			doc.add(new Field("contents", bodyText, Field.Store.YES,Field.Index.TOKENIZED) );
//			return doc;
//		}
//		return null;
		
	String bodyText = null;
	try {
				WordDocument wd = new WordDocument(is);
				StringWriter docTextWriter = new StringWriter();
				wd.writeAllText(new PrintWriter(docTextWriter));
				docTextWriter.close();
				bodyText = docTextWriter.toString();
			}
			catch (Exception e) {
				throw new Exception("Cannot extract text from a Word document"+"\n", e);
			}
			if ((bodyText != null) && (bodyText.trim().length() > 0)) {
				Document doc = new Document();
				doc.add(new Field("contents", bodyText, Field.Store.YES,Field.Index.TOKENIZED) );
				return doc;
			}
		return null;
		
				
	}
}
